#  -*- coding:utf-8 -*- 
"""
@ author: 罗金盛
@ time: 2024/7/16 
@ file: trajectory_simulation.py

"""
#获取滑动距离
import cv2
import os
def get_distance():

    #读取两张验证码图片
    bg = cv2.imread('new_bg_img.jpg')
    slice = cv2.imread('slice.jpg')
    #灰度化处理
    bg = cv2.cvtColor(bg,cv2.COLOR_BGR2GRAY)
    slice = cv2.cvtColor(slice,cv2.COLOR_BGR2GRAY)
    #图片边缘处理
    bg_can = cv2.Canny(bg,255,255)
    slice = cv2.Canny(slice,255,255)
    #匹配图像相似度
    r = cv2.matchTemplate(bg_can,slice,cv2.TM_CCOEFF_NORMED)
    #获取匹配度最好的结果
    minValue,MaxValue,minLoc,maxLoc = cv2.minMaxLoc(r)

    #测试滑动效果
    # x = maxLoc[0]
    # y = maxLoc[1]
    # bg = cv2.rectangle(bg,(x,y),(x+40,y+40),(255,255,255))
    # cv2.imshow('xxx',bg)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    return maxLoc[0] #返回匹配度最好的滑动距离结果
distance = get_distance()

#进行滑动轨迹的生成（算法）
def __ease_out_expo(sep):
    if sep == 1:
        return 1
    else:
        return 1 - pow(2, -10 * sep)
def get_slide_track(distance):
    import random
    '''
    根据滑动距离生成滑动轨迹
    :param distance:需要滑动的距离
    :return 滑动轨迹<type 'list'>:[[x,y,t],...]
        x:已滑动的横向距离
        y:已滑动的纵向距离，除起点外，均为0
        t:一次滑动消耗的时间，单位:毫秒
    '''
    if not isinstance(distance, int) or distance < 0:
        raise ValueError(f"distance类型必须是大于等于0的整数: distance: {distance}, type: {type(distance)}")
    #初始化滑动轨迹列表
    slide_track = [
        [random.randint(-50,-10),random.randint(-50,-10),0],
        [0,0,0],
    ]
    #共记录count次滑块位置信息
    count = 30 + int(distance/2)
    #初始化滑动时间
    t = random.randint(50,100)
    #记录上一次滑动的距离
    _x = 0
    _y = 0
    for i in range(count):
        #已滑动的横向距离
        x = round(__ease_out_expo(i / count) * distance)
        #滑动过程消耗的时间
        t += random.randint(10,20)
        if x == _x:
            continue
        slide_track.append([x,_y,t])
        _x = x
    slide_track.append([distance,0,t])

    return slide_track,t
#获取滑动轨迹和耗时
guiji,n = get_slide_track(distance)
print(guiji,n)
